﻿using System;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace Patcher
{
    public partial class Form1 : Form
    {
        
        private byte[] downloadedData;

        public Form1()
        {
            InitializeComponent();
        }

        private void downloadFile(string FTPAddress, string filename, string username, string password)
        {
            downloadedData = new byte[0];

            try
            {
                //Optional
                lstLog.Items.Add("Bağlanılıyor");
                Application.DoEvents();

                //Create FTP request
                //Note: format is ftp://server.com/file.ext
                FtpWebRequest request = FtpWebRequest.Create(FTPAddress + "/" + filename) as FtpWebRequest;

                //Optional
                lstLog.Items.Add("Bilgiler Alınıyor");
                Application.DoEvents();

                //Get the file size first (for progress bar)
                request.Method = WebRequestMethods.Ftp.GetFileSize;
                request.Credentials = new NetworkCredential(username, password);
                request.UsePassive = true;
                request.UseBinary = true;
                request.KeepAlive = true; //don't close the connection

                int dataLength = (int)request.GetResponse().ContentLength;

                //Optional
                lstLog.Items.Add("Liste indiriliyor");
                Application.DoEvents();

                //Now get the actual data
                request = FtpWebRequest.Create(FTPAddress + "/" + filename) as FtpWebRequest;
                request.Method = WebRequestMethods.Ftp.DownloadFile;
                request.Credentials = new NetworkCredential(username, password);
                request.UsePassive = true;
                request.UseBinary = true;
                request.KeepAlive = false; //close the connection when done

                //Set up progress bar
                prgDownload.Value = 0;
                prgDownload.Maximum = dataLength;
                lblBilgi.Text = "0 /" + dataLength.ToString();

                //Streams
                FtpWebResponse response = request.GetResponse() as FtpWebResponse;
                Stream reader = response.GetResponseStream();

                //Download to memory
                //Note: adjust the streams here to download directly to the hard drive
                MemoryStream memStream = new MemoryStream();
                byte[] buffer = new byte[1024]; //downloads in chuncks

                while (true)
                {
                    Application.DoEvents(); //prevent application from crashing

                    //Try to read the data
                    int bytesRead = reader.Read(buffer, 0, buffer.Length);

                    if (bytesRead == 0)
                    {
                        //Nothing was read, finished downloading
                        prgDownload.Value = prgDownload.Maximum;
                        lblBilgi.Text = dataLength.ToString() + "/" + dataLength.ToString();

                        Application.DoEvents();
                        break;
                    }
                    else
                    {
                        //Write the downloaded data
                        memStream.Write(buffer, 0, bytesRead);

                        //Update the progress bar
                        if (prgDownload.Value + bytesRead <= prgDownload.Maximum)
                        {
                            prgDownload.Value += bytesRead;
                            prgDownload.Text = prgDownload.Value.ToString() + "/" + dataLength.ToString();

                            prgDownload.Refresh();
                            Application.DoEvents();
                        }
                    }
                }

                //Convert the downloaded stream to a byte array
                downloadedData = memStream.ToArray();

                //Clean up
                reader.Close();
                memStream.Close();
                response.Close();

                lstLog.Items.Add("Liste İndirildi");
                if (downloadedData != null && downloadedData.Length != 0)
                {                   
                        lstLog.Items.Add("Liste Kaydediliyor");
                        Application.DoEvents();

                        //Write the bytes to a file
                        FileStream newFile = new FileStream("List.txt", FileMode.Create);
                        newFile.Write(downloadedData, 0, downloadedData.Length);
                        newFile.Close();
                    

                        lstLog.Items.Add("Liste Kaydedildi");
                                           
                }
                else
                    MessageBox.Show("Bir hata oluştu.");



            }
            catch (Exception)
            {
                MessageBox.Show("Bir hata oluştu.");
            }
        }

        private void downloadListFiles(string FTPAddress, string username, string password)
        {
            downloadedData = new byte[0];

            try
            {
                lstLog.Items.Add("Liste okunuyor...");
                StreamReader Reader1 = new StreamReader("List.txt");
                string downloadList = Reader1.ReadToEnd();
                lstLog.Items.Add("Liste okundu");

                FtpWebRequest request = FtpWebRequest.Create(FTPAddress + "/" + filename) as FtpWebRequest;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }


            
            
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            downloadFile("ftp://ftp.byethost22.com", "/htdocs/pa/List.txt", "b22_7813795", "b5xsrecbwn..");
            downloadListFiles("ftp://ftp.byethost22.com", "b22_7813795", "b5xsrecbwn..");
        }


    }
}
